********************************************************************************
*                                                                              *
*                            DETERMINE REPLACEABILITY                          *
*                                                                              *
********************************************************************************

use "H:\Robots and Workers\Base files\ISCO_SOC_Crosswalk.dta", clear
rename (SOCCode SOCTitle ISCO08Code ISCO08TitleEN) (soc soctitle isco08 isco08title)
drop Comment81711
capture drop replaceable
capture drop bluecollar

////////////////////////////////////////////////////////////////////////////////
// Based on Graetz & Michaels, 1990 occupational codes translated to ONET SOC //
// codes, see Dropbox                                                         //
////////////////////////////////////////////////////////////////////////////////

g replaceable = 0 
replace replaceable = 1 if soc == 472141
replace replaceable = 1 if soc == 472171
replace replaceable = 1 if soc == 472221
replace replaceable = 1 if soc == 474061
replace replaceable = 1 if soc == 474071
replace replaceable = 1 if soc == 475011
replace replaceable = 1 if soc == 475012
replace replaceable = 1 if soc == 475013
replace replaceable = 1 if soc == 475021
replace replaceable = 1 if soc == 475031
replace replaceable = 1 if soc == 475071
replace replaceable = 1 if soc == 512011
replace replaceable = 1 if soc == 512021
replace replaceable = 1 if soc == 512022
replace replaceable = 1 if soc == 512023
replace replaceable = 1 if soc == 512031
replace replaceable = 1 if soc == 512041
replace replaceable = 1 if soc == 512091 
replace replaceable = 1 if soc == 512092
replace replaceable = 1 if soc == 512093
replace replaceable = 1 if soc == 512099
replace replaceable = 1 if soc == 513091
replace replaceable = 1 if soc == 513093
replace replaceable = 1 if soc == 514021
replace replaceable = 1 if soc == 514022
replace replaceable = 1 if soc == 514023
replace replaceable = 1 if soc == 514031
replace replaceable = 1 if soc == 514032
replace replaceable = 1 if soc == 514033
replace replaceable = 1 if soc == 514034
replace replaceable = 1 if soc == 514035
replace replaceable = 1 if soc == 514051
replace replaceable = 1 if soc == 514052
replace replaceable = 1 if soc == 514071
replace replaceable = 1 if soc == 514072
replace replaceable = 1 if soc == 514081
replace replaceable = 1 if soc == 514111
replace replaceable = 1 if soc == 514121
replace replaceable = 1 if soc == 514122
replace replaceable = 1 if soc == 514191
replace replaceable = 1 if soc == 514193
replace replaceable = 1 if soc == 514194
replace replaceable = 1 if soc == 514199
replace replaceable = 1 if soc == 515022
replace replaceable = 1 if soc == 515023
replace replaceable = 1 if soc == 516021
replace replaceable = 1 if soc == 516042
replace replaceable = 1 if soc == 516061
replace replaceable = 1 if soc == 516062
replace replaceable = 1 if soc == 516063
replace replaceable = 1 if soc == 516064
replace replaceable = 1 if soc == 516091
replace replaceable = 1 if soc == 516092
replace replaceable = 1 if soc == 516099
replace replaceable = 1 if soc == 517041
replace replaceable = 1 if soc == 517042
replace replaceable = 1 if soc == 519011
replace replaceable = 1 if soc == 519012
replace replaceable = 1 if soc == 519021
replace replaceable = 1 if soc == 519022
replace replaceable = 1 if soc == 519023
replace replaceable = 1 if soc == 519031
replace replaceable = 1 if soc == 519032
replace replaceable = 1 if soc == 519041
replace replaceable = 1 if soc == 519051
replace replaceable = 1 if soc == 519111
replace replaceable = 1 if soc == 519121
replace replaceable = 1 if soc == 519122
replace replaceable = 1 if soc == 519123
replace replaceable = 1 if soc == 519141
replace replaceable = 1 if soc == 519191
replace replaceable = 1 if soc == 519192
replace replaceable = 1 if soc == 519193
replace replaceable = 1 if soc == 519194
replace replaceable = 1 if soc == 519195
replace replaceable = 1 if soc == 519196
replace replaceable = 1 if soc == 519197
replace replaceable = 1 if soc == 519199
replace replaceable = 1 if soc == 537011
replace replaceable = 1 if soc == 537062
replace replaceable = 1 if soc == 537063
replace replaceable = 1 if soc == 537064
replace replaceable = 1 if soc == 537111
replace replaceable = 1 if soc == 537121
replace replaceable = 1 if soc == 537199

// YOU MAY WANT TO CHECK HERE THE RELATIONSHIP BETWEEN SOC AND ISCO08 replaceability

//drop soc soctitle
duplicates drop isco08, force
sort isco08

////////////////////////////////////////////////////////////////////////////////
// Obtain RTI                                                                 //
////////////////////////////////////////////////////////////////////////////////

rename isco08 ISCO08Code 
merge 1:1 ISCO08Code using "H:\Robots and Workers\Base files\onet-ISCO08RTI_1999.dta", nogen keepusing(RTI99)
rename (ISCO08Code RTI99) (isco08 rti)
g routine = rti>1 if rti!=.


////////////////////////////////////////////////////////////////////////////////
// Determine blue-collar status                                               //
////////////////////////////////////////////////////////////////////////////////

merge m:1 isco08 using "H:\Robots and Workers\Base files\bluecollar_new.dta", nogen keepusing(bluecollar isco08descr)
// Change missings in blue collar occupations
replace bluecollar = 0 if isco08==100
replace bluecollar = 0 if isco08==200
replace bluecollar = 0 if isco08==300
replace bluecollar = 0 if isco08==211
replace bluecollar = 0 if isco08==315
g meffect = bluecollar == 1 & routine==1 if routine !=. & bluecollar !=.

////////////////////////////////////////////////////////////////////////////////
// Adjust missings and correct a few occupations                              //
////////////////////////////////////////////////////////////////////////////////

sort isco08
// Keep original replaceability index and adjust and update below
g replaceable_orig = replaceable 

// Fill in missing replaceability for ISCO that does not merge with ONET SOC.
replace replaceable = 1 if isco08 == 7200 & replaceable==.
replace replaceable = 1 if isco08 == 7210 & replaceable==.
replace replaceable = 1 if isco08 == 7220 & replaceable==.
replace replaceable = 1 if isco08 == 7520 & replaceable==.
replace replaceable = 1 if isco08 == 8000 & replaceable==.
replace replaceable = 1 if isco08 == 8100 & replaceable==.
replace replaceable = 1 if isco08 == 8110 & replaceable==.
replace replaceable = 1 if isco08 == 8120 & replaceable==.
replace replaceable = 1 if isco08 == 8130 & replaceable==.
replace replaceable = 1 if isco08 == 8140 & replaceable==.
replace replaceable = 1 if isco08 == 8150 & replaceable==.
replace replaceable = 1 if isco08 == 8170 & replaceable==.
replace replaceable = 1 if isco08 == 8180 & replaceable==.
replace replaceable = 1 if isco08 == 8200 & replaceable==.
replace replaceable = 1 if isco08 == 8210 & replaceable==.
replace replaceable = 1 if isco08 == 9320 & replaceable==.
replace replaceable = 0 if replaceable==.

// Make a few adjustments
replace replaceable = 1 if isco08 == 7213 // Sheet-metal workers
replace replaceable = 0 if isco08 == 7514 // Fruit, vegetable and related preservers
replace replaceable = 1 if isco08 == 8114 // Cement, stone and other mineral products machine operators
replace replaceable = 1 if isco08 == 8153 // Sewing machine operators
replace replaceable = 1 if isco08 == 8157 // Laundry machine operators

order isco08* replaceable_orig replaceable bluecollar

do "H:\Robots and Workers\Do-files\Variable labels.do"
save "H:\Robots and Workers\Base files\Replaceable.dta", replace




//Cheking the potential bias in bluecollar workers in the original file 
//use "H:\Robots and Workers\Base files\ISCO_SOC_Crosswalk.dta", clear
//rename (SOCCode SOCTitle ISCO08Code ISCO08TitleEN) (soc soctitle isco08 isco08title)
//drop Comment81711
//capture drop replaceable
//capture drop bluecollar

// Checking the blue collar workers after we create replaceability
//merge m:1 soc using "H:\Robots and Workers\Base files\BluecollarSoc.dta"
//unique soc if bluecollar==1 // 140 soc bc
//unique isco08 if bluecollar==1 // 102 isco08 bc
//clear

// Now run the do file from the beginning until save command. Then repeat below
//merge m:1 soc using "H:\Robots and Workers\Base files\BluecollarSoc.dta"
//unique soc if bluecollar==1 // 165 soc bc
//unique isco08 if bluecollar==1 // 125 isco08 bc


